<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 权限检测，更新方法都在这。
 *
 * @ auth yunyun
 */

class Root_lib {
	
	var $CI				= null;
	var $uri_array			= array();
	var $all_insert_array 		= array();
	var $total_info				= 0;
	
	var $cut_path			= array(); 			//不检测权限的文件路径
	var $error_login_url		= 'login';			//错误返回路径
	var $error_return_url		= 'remind/index/error';			//错误返回路径
	var $success_return_url		= 'main';			//成功返回路径
	var $file_path 			= '../application/controllers';
	
	var $session_message 		= 'return_message_info';//
	var $session_login_message 		= 'error_info_message';

	//var $root_url = "admin/menu/v1/menu/menu_check";
	
	function __construct($config = array()) 
	{
    	$this->CI = & get_instance();
    	
    	if (count($config) > 0)
		{
			$this->initialize($config);
		}
		
		$this->uri_array = array();
		$this->all_insert_array 		= array();
		$this->total_info = 0;
		
        log_message('debug', "root_lib Class Initialized");
    }
    
	//检测权限
	public function check($str = '')
	{
		$res_data = $this->_check($str);
		if($res_data['type'] != 200)
		{
			if($res_data['type'] == 300)
			{
				//登录问题
				$this->CI->session->set_userdata($this->session_login_message,$res_data['message']);
				redirect($this->error_login_url);
				exit();
			}
			
			$this->CI->session->set_flashdata($this->session_message,$res_data['message']);
			redirect($this->error_return_url);
			exit();
		}
	}
	
	//状态 bool json array
	public function state($str = '',$type = "bool")
	{
		$res_data = $this->_check($str);
		if($type == "bool")
		{
			if($res_data['type'] == 200)
			{
				return true;
			}
			return false;
		}
		else if($type == "array")
		{
			return $res_data;
		}
		return $res_data;
		
	}
	
	public function login($type = false)
	{
		$ret_data["type"] = 200;
		$ret_data["message"] = "成功";
		
		//检测登录
		$this->CI->load->library('login_lib');
		
		if(!$this->CI->login_lib->check())
		{
			$ret_data["type"] = 300;
			$ret_data["message"] = "没有找到你的登录信息";
			if($type == false)
			{
				return $ret_data;
			}
			else
			{
				$this->CI->session->set_userdata($this->session_login_message,$ret_data['message']);
				redirect($this->error_login_url);
				exit();
			}
			
		}
		if($type == false)
		{
			return $ret_data;
		}
		
	}
	
	/**
	 * 私有检测
	 * 
	 * @param unknown_type $date_str
	 */
	private function _check($date_str = '')
	{
		$ret_data["type"] = 200;
		$ret_data["message"] = "成功";
		
		//检测登录
		$this->CI->load->library('login_lib');
		
		if(!$this->CI->login_lib->check())
		{
			$ret_data["type"] = 300;
			$ret_data["message"] = "没有找到你的登录信息";
			return $ret_data;
			
		}
		
		//检测权限
		$root_type_str = $this->CI->login_lib->data("type_root");
		if($root_type_str != "#all")
		{
			$this->CI->load->model("menu/menu_model");
			$menu_data = $this->CI->menu_model->get_key_data($date_str,$root_type_str);
			
			if($menu_data)
			{
				$ret_data["type"] = 500;
				$ret_data["message"] = "你没有这个菜单权限";
				return $ret_data;
			}
			
		}
		return $ret_data;
	}
		
		
	
	/**
	 * Initialize preferences
	 *
	 * @access	public
	 * @param	array
	 * @return	void
	 */
	function initialize($config = array())
	{
		foreach ($config as $key => $val)
		{
			if (isset($this->$key))
			{
				$this->$key = $val;
			}
		}

	}
	
	
	/**
     * 将字符串转换为数组，并反向键值关系
	 *
     * @param string $str
     */
    private function str2array($str)
    {
        $ret = explode(',', $str);
        return array_flip($ret);
    }
}